//
//  HomeVC.m
//  PlayShow
//
//  Created by hff on 2018/4/20.
//  Copyright © 2018年 threeTi. All rights reserved.
//

#import "HomeVC.h"
#import "SDCycleScrollView.h"
#import "UIColor+Utils.h"
#import "AppMacro.h"
#import "HomeListCell.h"
#import "CourseDetialVC.h"
#import "HUD.h"

@interface HomeVC ()<SDCycleScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>


@property (strong, nonatomic)  UIView *topAdsBngView;//轮播背景
@property (strong, nonatomic) SDCycleScrollView *cycleScrollView;//轮播图

@property (weak, nonatomic) IBOutlet UITableView *listTableView;//课程列表
@property (strong, nonatomic) NSMutableArray *dataArray;//课程数据
@property (strong, nonatomic) NSMutableArray *imageArray;//主讲人头像
@property (strong, nonatomic) NSMutableArray *titleArray;//主讲课程标题
@property (strong, nonatomic) NSMutableArray *speakerArray;//主讲人姓名

@end

@implementation HomeVC

-(UIView *)topAdsBngView
{
    if (!_topAdsBngView) {
        _topAdsBngView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, UIScreenWidth/3)];
    }
    return _topAdsBngView;
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"樱桃商学院";
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(110, 8, (UIScreenWidth-110)/2, 25)];
    //PINK_COCOR
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 26, 26)];
    imageView.layer.cornerRadius = 13;
    [imageView.layer setMasksToBounds:YES];
    [imageView setImage:[UIImage imageNamed:@"userHead"]];
    [titleView addSubview:imageView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, 100, 25)];
    titleLabel.text = @"樱桃商学院";
    [titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
    [titleLabel setTextColor:[UIColor colorForHex:PINK_COCOR]];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [titleView addSubview:titleLabel];
    self.navigationItem.titleView = titleView;
    
    //注册自定义cell
    [self.listTableView registerNib:[UINib nibWithNibName:@"HomeListCell"  bundle:nil] forCellReuseIdentifier:@"HomeListCell" ];
    [self.listTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.listTableView setBackgroundColor:[UIColor clearColor]];
    
    self.dataArray = [NSMutableArray arrayWithObjects:@"0", @"0", @"0", @"0", nil];
    self.imageArray = [NSMutableArray arrayWithObjects:@"cycleImage_1", @"cycleImage_2", @"cycleImage_3", @"cycleImage_4",  nil];
    self.titleArray = [NSMutableArray arrayWithObjects:@"《金融市场与现代金融制度》", @"《会计与管理控制常见问题》", @"《博弈论在现代管学中的应用》", @"《美联储与金融危机》",  nil];
    self.speakerArray = [NSMutableArray arrayWithObjects:@"罗伯特.泰勒", @"埃韦尔.施拖洛韦", @"本.波拉克", @"本.伯南克",  nil];
   
    //添加轮播图
    [self.listTableView setTableHeaderView:self.topAdsBngView];
    [self setupCycleScrollView];
}


-(void)setupCycleScrollView
{
    //添加轮播图
    _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, UIScreenWidth, UIScreenWidth/3) delegate:self placeholderImage:[UIImage imageNamed:@""]];
    // 设置pageControl居右
    _cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    // 自定义轮播时间间隔
    _cycleScrollView.autoScrollTimeInterval = 4;
    _cycleScrollView.currentPageDotColor = [UIColor colorForHex:PINK_COCOR];
    _cycleScrollView.pageDotColor = [UIColor whiteColor];
    
    [self.topAdsBngView addSubview:self.cycleScrollView];
    
    
    NSMutableArray *imageArray = [NSMutableArray arrayWithObjects:@"cycleImage_1",@"cycleImage_2",@"cycleImage_3",@"cycleImage_4", nil];
    _cycleScrollView.imageURLStringsGroup = imageArray;
}


#pragma mark *** 轮播图回调方法 ***
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"轮播图点击事件");
    //[HUD showAlertWithTitle:[NSString stringWithFormat:@"点击了%ld页",index]];
    CourseDetialVC *detailVC = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"CourseDetialVC"];
    detailVC.catalogTag = index;
    [self.navigationController pushViewController:detailVC animated:YES];
}

/** 图片滚动回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index
{
    
}

#pragma mark *** tableView_delegate  ***
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeListCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.contentView setBackgroundColor:[UIColor whiteColor]];
    
    [cell.headImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"speakerHead_%ld",indexPath.row+1]]];
    [cell.titleLabel setText:[self.titleArray objectAtIndex:indexPath.row]];
    [cell.speakerLabel setText:[self.speakerArray objectAtIndex:indexPath.row]];
    
    return cell;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  [self.dataArray count];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CourseDetialVC *detailVC = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"CourseDetialVC"];
    detailVC.catalogTag = indexPath.row;
    [self.navigationController pushViewController:detailVC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
